package com.li.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.entity.PageResult;
import com.li.entity.QueryPageBean;
import com.li.mapper.RoleMapper;
import com.li.pojo.Role;
import com.li.service.RoleService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Override
    public void add(Role role, String[] ids) {
        //查询角色名是否存在
        Role r = roleMapper.findByName(role.getName());
        //如果存在删除原有的角色与所关联的权限
        if(r!=null){
            //删除关联权限
            roleMapper.deleteRoleAndPermissions(r.getId());
            //删除对应角色
            roleMapper.deleteById(r.getId());
        }
        //进行新增操作
        roleMapper.add(role);
        //增加对应的权限
        for (String id : ids) {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("roleId",role.getId());
            map.put("permissionId",Integer.parseInt(id));
            roleMapper.addRoleAndPermissions(map);
        }
    }

    @Override
    public PageResult findByCondition(QueryPageBean queryPageBean) {

        //开启分页
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //获取数据
        Page<Role> list = roleMapper.findByCondition(queryPageBean.getQueryString());
        //执行封装
        PageResult pageResult = new PageResult();
        pageResult.setTotal(list.getTotal());
        pageResult.setRows(list.getResult());
        //返回数据
        return pageResult;
    }

    @Override
    public void delete(Integer ids) {
        //删除角色所关联的菜单
        roleMapper.deleteRoleAndMenu(ids);
        //删除角色所关联的权限
        roleMapper.deleteRoleAndPermissions(ids);
        //根据id删除角色
        roleMapper.deleteById(ids);

    }

    @Override
    public Role findById(Integer ids) {
        return roleMapper.findById(ids);
    }

    @Override
    public void update(Role role, Integer[] permissionIds, Integer[] menuId) {
        //删除角色所关联的权限
        roleMapper.deleteRoleAndPermissions(role.getId());
        //删除角色所关联的菜单
        roleMapper.deleteRoleAndMenu(role.getId());
        //新增角色关联的权限
        for (Integer permissionId : permissionIds) {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("roleId",role.getId());
            map.put("permissionId",permissionId);
            roleMapper.addRoleAndPermissions(map);
        }
        //新增角色关联的菜单
        for (Integer integer : menuId) {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("roleId",role.getId());
            map.put("menuId",integer);
            roleMapper.addRoleAndMenu(map);
        }
        //更新角色
        roleMapper.update(role);
    }

    @Override
    public List<Role> findAll() {
        return roleMapper.findAll();
    }
}
